package com.x_memteor.base_master.utils;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;

import com.x_memteor.base_master.application.MyApplication;

public class SoundPlayManager {
    private static final SoundPlayManager ourInstance = new SoundPlayManager();
    private MediaPlayer mediaPlayer = new MediaPlayer();

    private static SoundPlayManager mSoundPlayManager = null;

    private SoundPlayManager() {
    }

    public static SoundPlayManager getInstance() {
        synchronized (SoundPlayManager.class) {
            if (mSoundPlayManager == null) {
                mSoundPlayManager = new SoundPlayManager();
            }
        }
        return mSoundPlayManager;
    }

    public void add(String resId) {
        play(resId);
    }

    private void play(String resId) {
        try {
            this.mediaPlayer.reset();
            AssetFileDescriptor fileDescriptor = MyApplication.getInstance().getAssets().openFd(resId);
            this.mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
            this.mediaPlayer.prepare();
            fileDescriptor.close();
            this.mediaPlayer.start();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}